﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Xml;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Tasx.Definitions;

namespace Tasx.ViewModels
{
	public class TaskListsViewModel : ViewModelBase
	{
		public TaskListsViewModel()
		{
		    MessengerInstance.Register<SyncedMessage>(this, message => PopulateAfterSync(message.Proxy));
		}

        private TaskList2 _selectedList = null;
	    public TaskList2 SelectedList
	    {
            get { return _selectedList; }
            set
            {
                if (_selectedList == value)
                {
                    return;
                }

                _selectedList = value;
                RaisePropertyChanged(() => SelectedList);
                MessengerInstance.Send(new SelectedListChangedMessage(SelectedList));
            }
	    }

        private ObservableCollection<TaskList2> _lists = new ObservableCollection<TaskList2>();
        public ObservableCollection<TaskList2> Lists
		{
			get
			{
				return _lists;
			}
		}
		
		public ICommand CreateList
		{
			get { return new RelayCommand<object>(p => ExecuteCreateList(), p => CanExecuteCreateList()); }
		}
		private void ExecuteCreateList()
		{
			Lists.Add(new TaskList2 { Name = "New List" });
		}
		private bool CanExecuteCreateList()
		{
			return true;
		}

        private void PopulateAfterSync(TasksProxy proxy)
        {
            Lists.Clear();
            proxy.Lists.ToList().ForEach(list => Lists.Add(list));
        }
	}

    public class SelectedListChangedMessage
    {
        public SelectedListChangedMessage(TaskList2 list)
        {
            List = list;
        }

        public TaskList2 List { get; private set; }
    }
}
